/*var par_setting = {
    "ele":$('#<?php echo $G_MODEL_PROCESS_NAME; ?>_table'),
    "title":["工步状态*","工作时间*(min)","电流*(mA)","上限电压*(mV)","下限电压(mV)*","ΔV(mV)","终止电流(mA)","容量异常","CCT下限(s)","CCT上限(s)","Time Over"], 
    "row": 32, // 每页显示最大行数
};*/

// 工步设定表
function process_table(par){
	var obj ={};
	// 所有工步配置
	var station_array={
		"1":"station",
		"2":"time",
		"3":"current",
		"4":"upperVoltage",
		"5":"lowerVoltage",
		"6":"stop_DeltaVoltage",
		"7":"stop_Current",
		"8":"stop_Capacity",
		"9":"cct_begin",
		"10":"cct_end",
		"11":"time_over",
		'12':"beginStep",
		'13':"endStep",
		'14':"times",
		'15':"contact_check",
	 	'16':"temp_supp_point",
	 	'17':"proteceted_capacity_max"
	};
	var lope_array = ['loop_time','start_process','end_process'];

	// 状态值
	// obj.end =false; 

	// 标题数组
	var title=['工步'];
	title=title.concat(par.title);

	// ui 计算
	var col=title.length;
	var row = par.row ;
	var ele_w = par.ele.width();
	var ele_h = par.ele.height();
	var row_height = (ele_h*0.93) / row;
	var html_body_one_row =  '<tr><td class="td_id"style="height:'+row_height+'px"></td>';
	var col_width = (ele_w*0.98-2) / col;
   
	// ui & html
	var obj_ui_conf={
		'html_body_one_row' :html_body_one_row,
 		'ele' : par.ele ,
		'ele_w' : ele_w, 
		'ele_h' : ele_h , 
		'col' : col,
		'title' : title,
		'len': 0 ,			//	更新到当前设置的最大工步数, 用于校验工步补偿点
		'row' : row , 
		'col_width' : col_width,
		'row_height' : row_height ,
		'input_html' : '<input type="text" class="w h editor_mode btn-block text-center" value="">' ,
		'alert_type' : '<select name="" class="w h editor_mode alert_type btn-block text-center">\
				<option class="h"  value="" ></option class="h" ><option class="h"  value="OFF" >OFF</option class="h" >\
				<option class="h"  value="TYPE_A">TYPE_A</option class="h" >\
				<option class="h"  value="TYPE_B">TYPE_B</option class="h" >\
			</select>',

		'select_html' : '<select class="editor_mode h w procee_status btn-block text-center h">\
					<option class="h"  value="3" >搁置</option>\
					<option class="h"  value="2">恒流恒压充电</option >\
					<option class="h"  value="4">恒流放电</option >\
					<option class="h"  value="1">恒流充电</option>\
					<option class="h"  value="5">循环</option>\
					<option class="h"  value="6">结束</option>\
				</select>',
		'loop_type' : '<td name="td_data" class="num12" colspan="10" style="display:none">\
			<div class="col-sm-4 h border_r loop_time">\
			<div class="col-sm-7 h ver_center">循环次数</div>\
			<input class="col-sm-5 h ver_center editor_mode bor" value="" style="display: none;">\
			<span class="col-sm-5 h ver_center bor text_mode" style=""></span>\
			</div>\
			<div class="col-sm-4 h border_r start_process">\
			<div class="col-sm-7 h ver_center">起始工步</div>\
			<input class="col-sm-5 h ver_center editor_mode bor" value="" style="display: none;">\
			<span class="col-sm-5 h ver_center bor text_mode" style=""></span>\
			</div>\
			<div class="col-sm-4 h border_r end_process">\
			<div class="col-sm-7 h ver_center">结束工步</div>\
			<input class="col-sm-5 h ver_center editor_mode bor" value="" style="display: none;">\
			<span class="col-sm-5 h ver_center bor text_mode" style=""></span>\
			</div></tr>',
		'select_html_2' :'<div class="text_mode h ver_center"></div>' ,
	};
    
	obj_ui_conf.o_html = get_o_html();
	obj_ui_conf.html_body = get_html_body();

	var value_to_station = {
		"1" : "恒流充电",
		"2" : "恒流恒压充电",
		"3" : "搁置",
		"4" : "恒流放电",
		"5" : "循环",
		"6" : "结束"
	};	
	// 必填项
	var fill_td ={
		'3' : [2,4,5],
		'2' : [2,3,4,5,7],
		'4' : [2,3,4,5],
		'1' : [2,3,4,5],
		'5' : [12],
		'6' : '' 
	};
	// 显示项
	var show_td ={
		'3' : [2,4,5],
		'2' : [2,3,4,5,6,7,8,9,10],
		'4' : [2,3,4,5,8],
		'1' : [2,3,4,5,6,8],
		'5' : [12],
		'6' : '' 
	};
	var obj_mapped ={
		'value_to_station' : value_to_station,
		'show_td' : show_td,
		'fill_td' : fill_td,
	};
	// 所有方法
	var obj_fn = {
		'init_ui' : init_ui,	// 绘制表头
		'init_tr' : init_tr,	// 初始化行
		'clear_tr_class' : clear_tr_class ,		//	 清除行颜色
		'clear_data' : clear_data ,
		'get_process_array' : get_process_array ,
		'get_single_json' : get_single_json ,
		'pack_json' : pack_json ,
		// 'array_unique' : array_unique,
		'clear_ui' : clear_ui ,
		'validate_data' : validate_data,

		'switch_css' :switch_css,
		'switch_css_all' :switch_css_all, 	
		'change_event' : change_event,		// 切换输入框显示
		'select_change' : select_change,	
		'get_json' : get_json ,
		'process_parse' : process_parse ,
		'num1_click' : num1_click ,
		'other_td_click' : other_td_click ,
		'keyboard_event' : keyboard_event ,
		'binding_event' : binding_event ,
		'init_table' : init_table ,
		'show_text_td' : show_text_td ,
		'data_obj' : data_obj ,
		'reprocessing_obj' : reprocessing_obj ,
		'context_click' : context_click ,
		'blink_element' : blink_element ,
		'update_table_idx' : update_table_idx ,
		'num11_change' : num11_change ,
		'init_history_table' : init_history_table,
	};
	var obj = Object.assign(obj_ui_conf , obj_mapped , obj_fn);

    if(obj_ui_conf.ele.length==0){
    	swal('工步设定表指定元素不存在','','error');
    	return;
    }

	obj_fn.init_table();
	// 清空行颜色
    function clear_tr_class(ele){
		for (let j = 1; j <=4;j++) {
			ele.removeClass('station'+j);
		}
    }
    function init_ui(){
        // create table_thead 
        par.ele.html('');
        par.ele.html(obj_ui_conf.o_html);

        var title_ele =par.ele.find('.bottom_title_text');
        var bottom_title_text= title_ele.closest('.half_h').height();
        title_ele.css('line-height',bottom_title_text+'px');

        var html_body ='';
   	    for (let i = 1; i<=obj_ui_conf['row']*1; i++) {
            html_body+=obj_ui_conf['html_body'];
        }

        par.ele.find(".table_data tbody").html(html_body);
        par.ele.find(".table_data tbody .td_id").each(function(a,b){
            $(b).html(a+1);
        });
        par.ele.find(".table_data [name='td_data']").each(function(a,b){
            $(b).find('.editor_mode,.text_mode').hide();
        });
        par.ele.find(".table_data .num12").each(function(a,b){
            $(b).hide();
        });
        // 禁止选中
        // document.getElementById('process_test').onselectstart = function(){return false;}
    }

    // init_tr = clear_ui +  clear_data 
    function init_tr(ele){
    	clear_ui(ele);
    	clear_data(ele);
    	ele.attr('mode','none');
    }

	// 切换模式
	// ele 切换的行元素
	// station 工步值
	// [可选参数] 
	//  par==0 文本模式
	//  par==1 编辑模式 
	function switch_css (ele,station,par){
		var mode  = ele.attr('mode');
		if( 1>(station*1) || (station*1)>6){
			return;
		}
		// console.log(mode,station,par);
		// 转为编辑模式
		if (par ===1) {
			// 根据工步显示对应的td
			show_edit_td(ele,station);
			return;
		// 转为文本模式
		}else if (par ===0 ) {
			var tmp_station =ele.find('.num1 .editor_mode').val();
			if(tmp_station=='6'){
				station=tmp_station;
			}
			show_text_td(ele,station);	
			return;
		}else if ( mode=='edit') {
			// console.log(ele,station);
			show_text_td(ele,station);					
			return;
		}else if( mode=='text' || mode=='none' ){				
			// 根据工步显示对应的td
			show_edit_td(ele,station);
			return;
		}
	}

	

	function num11_change(){
		par.ele.find('.num11 select').each(function(a,b){
			$(b).on('change',function(){
				var ele = $(b).closest('td');
				var tr = $(b).closest('tr');
				ele.find('.text_mode').attr('val_tag',$(b).val()) ;
			});
		});
	}
	// 转为文本模式
	function show_text_td (ele,station){
		clear_tr_class(ele);

		if(station=='6'){
			 ele.find('[name="td_data"]').show();
			 ele.find('[name="td_data"] .text_mode').show();
			 ele.find('[name="td_data"] .editor_mode').hide();
			 ele.find('.num1').show();
			 ele.find('.num12').hide();

			 clear_data(ele);
		}else if(station=='5'){
			ele.find('.editor_mode').each(function(a,b){
				$(b).siblings('.text_mode').html($(b).val());
				$(b).hide();
				$(b).siblings('.text_mode').show();
			});

		}else{
			ele.find('.editor_mode').each(function(a,b){
				if($(b).closest('td').hasClass('num1') ){	 	// 	必填字段 1
					var val = $(b).val();
					var html = $(b).find('option:selected').html();
					if(html!=null && html!=undefined && html!=''){
						$(b).siblings('.text_mode').html(html);
						$(b).siblings('.text_mode').attr('val_tag',val);
						$(b).siblings('.text_mode').show();
						$(b).hide();
					}
				}else if($(b).closest('td').hasClass('num11') ){		// 	非必填字段 11
					var val = $(b).val();
					$(b).siblings('.text_mode').attr('val_tag',val);
					$(b).siblings('.text_mode').html(val);
					$(b).siblings('.text_mode').show();
					$(b).hide();
				}
				else{
					//  非必填字段 6-10 		//  必填字段 2-5
					var val = $(b).val();
					$(b).siblings('.text_mode').html(val);
					$(b).siblings('.text_mode').show();
					$(b).hide();
				}
			});
			ele.addClass('station'+station);
		}
		ele.find('.num1 .text_mode').attr('val_tag',station);
		ele.find('.num1 .text_mode').html(value_to_station[station]);
		ele.attr('mode','text');
	}
	// 转为编辑模式
	function show_edit_td(ele,station){
		// 初始化行内单元格
		ele.find('td[name="td_data"] .editor_mode').each(function(a,b){
			$(b).find('.editor_mode').val('');
		});
		ele.find('td[name="td_data"] .text_mode').hide();
		
		station = station*1;
		if(station == 6){
			clear_ui(ele);
		}else{
			// if(obj.end){
			// 	obj.end =false;
			// }
			if(station == 5){
				ele.find('td[name="td_data"]').hide();
				ele.find('.num12').show();
				ele.find('.num12 .editor_mode').show();
				ele.find('.num12 .text_mode').hide();
				ele.find('.num12 .editor_mode').each(function(c,d){
					$(d).val($(d).siblings('.text_mode').html());
				});

			}else{
				ele.find('td[name="td_data"]').show();
				ele.find('.num12').hide();
				ele.find('td[name="td_data"]:not(.num12,.num11)').each(function(c,d){
					$(d).find('.editor_mode').show();
					$(d).find('.text_mode').hide();
					$(d).find('.editor_mode').val($(d).find('.text_mode').html());
				});
				ele.find('.num11').find('.editor_mode').show();
				ele.find('.num11').find('.text_mode').hide();
				ele.find('.num11 .editor_mode').val(ele.find('.num11 .text_mode').attr('val_tag'));
			}
		} 

		ele.find('.num1').show();
		ele.find('.num1 .text_mode').attr('val_tag',station);

		ele.find('.num1').each(function(a,b){
			$(b).find('.editor_mode').val(station);
			$(b).find('.editor_mode').show();
			$(b).find('.text_mode').hide();
		});
		ele.find('.text_mode').html('');
		ele.attr('mode','edit') ;
	}
	

	

	
	
	function switch_css_all (){
		par.ele.find('.table_data tr').each(function(a,b){
			$(b).find('td.num1').each(function(c,d){
				if($(d).find('.editor_mode').css('display')!='none'){
					var ele =$(d).closest('tr');
					obj_fn.switch_css(ele,ele.find('.num1 .editor_mode').val(),0);
				}
			});
		});
	}

	// 切换下拉
	function change_event (){
		par.ele.find('.table_data').on('change','.procee_status',function(){
			if( station<1 || station >6 ){
				return false;
			}
			let ele = $(this).closest('tr'); 
			if(ele.attr('mode')=='edit'){
				var station = $(this).val();
				if(station=='6'){
                    swal({
                        title:'<h3>确认结束?</h3>',
                        html:'<span>此操作会清除结束后的全部工步</span>',
                        type:'warning',
                        showCancelButton: true,
                        confirmButtonText:'确定',
                        cancelButtonText:'取消',
                    }).then((result) => {
                      
                      if (result.value) {
		                	select_change($(this),station);
		                	show_text_td(ele,'6');

		                	ele.next('tr').each(function(a,b){
		                		var tr = $(b);
		                		init_tr(tr);
		                	});
							
		                }else{		//	退回上一个工步
		                	var last_station = ele.find('.num1 .text_mode').attr('val_tag') ;
		                	for (let j = 1; j <=4;j++) {
								ele.removeClass('last_station'+j);
							}
		                	if(last_station!='6'){
			                	if(last_station=='5'){
			                		ele.find('td.num12').show();
			                		ele.find('[name="td_data"]:not(.num12)').hide();
			                		// debug
			                		ele.find('td.num12 .text_mode').hide();
			                		ele.find('td.num12 .editor_mode').show();
			                	}else{
			                		ele.addClass('last_station'+last_station);
			                		ele.find('td.num12').hide();
			                		ele.find('[name="td_data"]:not(.num12)').show();
			                		ele.find('[name="td_data"]:not(.num12) .text_mode').hide();
			                		ele.find('[name="td_data"]:not(.num12) .editor_mode').show();
			                	}
			                	switch_css(ele,last_station,1);
			                	switch_css_all();
			                	switch_css(ele,last_station,1);
		                	}
		                }
                    })
                    
				}else{
					select_change($(this),station);
				}
			}
		});
	}

	function select_change (select_ele,station){
		var ele = '';
		if(!(select_ele.is('tr'))){
			ele = select_ele.closest('tr');
		}else{
			ele = select_ele;
		}
		
		init_tr(ele);
		switch_css(ele,station,1);

		if( 0<(station*1)<5 ){
			ele.addClass('station'+station);
		}
		ele.attr('mode','edit');
	}


	// 解析json为工步
	// 允许渲染空行
	// 有id则渲染, station 值为''时渲染空行
	function process_parse(json){
		// json 解析
		if(typeof json=='string'){
			try{
                
				json=JSON.parse(json);
			}catch(e){
				swal('json格式错误','',error);
				return;
			}
		}
        if(json['process']){
			json = json['process'];
		}
        
		// // 初始化ui
		obj.init_table();
		if(json.length==0){
			return;
		}

		// 渲染额外内容 取第一位
		var con =json[0]['contact_check']?json[0]['contact_check']:'';
		var temp =json[0]['temp_supp_point']?json[0]['temp_supp_point']:'';
		$('.contact_check').val(con);
		$('.temp_supp_point').val(temp);

		var exist_end_station =false ;	//	检测是否存在结束工步 ,没有则自动添加
		
		for(let j=0;j<json.length;++j){
			//text_mode
			var new_ele =par.ele.find('.table_data tr').eq(j);
			var station = json[j]['station'];
			var station_text;	
			if(station == '' || (station*1)<1 || (station*1)>6 || station === undefined){		//	空行
				init_tr(new_ele);
				continue;
			}

			if(station=='5'){
				new_ele.find('[name="td_data"]').hide();
				new_ele.find('.num12').show();
				new_ele.find('.num12 .text_mode').show();
				var bs = json[j][station_array[12]];
				var es = json[j][station_array[13]];
				var tm = json[j][station_array[14]];
				new_ele.find('.num12 .loop_time .text_mode').html(tm);
				new_ele.find('.num12 .start_process .text_mode').html(bs);
				new_ele.find('.num12 .end_process .text_mode').html(es);

			}else if(station=='6'){
				exist_end_station = true ;
			}else{
				for(let i= 2;i<=11;++i){
					i=i+'';
					if(json[j][station_array[i]]!=undefined && json[j][station_array[i]]!='' ){
						eval('var num'+i+'='+ '"' +(json[j][station_array[i]])+ '"');
						// if(eval("num"+i)!='undefined' && eval("num"+i)!=''){
				    	new_ele.find('.num'+i+" .text_mode").html(eval("num"+i));
				    	if(i=='11'){
					    	new_ele.find('.'+eval('num'+i)+" .text_mode").attr('val_tag',num11);
				    	}
						// }
						new_ele.find('.num'+i+" .text_mode").show();	
					}
					
					
				}
				new_ele.addClass('station'+station); 
			}

			// 渲染 num1 
			new_ele.find('.num1').show();
			new_ele.find(".num1 .text_mode").attr('val_tag',station);
			station_text = value_to_station[json[j]['station']];
			new_ele.find(".num1 .text_mode").html(station_text);
			new_ele.find(".num1 .text_mode").show();
			new_ele.attr('mode','text');
		}

		switch_css_all();

	

		if(!exist_end_station){
			var next_tr = par.ele.find('.table_data tr').eq((json.length)*1);
			switch_css(next_tr,'6',0); 
			obj_ui_conf.len = json.length;

		}else{
			obj_ui_conf.len= (json.length)*1-1;
		}
	}


	function get_single_json (ele){
		var single_json = {};
		var end_flag = 0 ;
		var station = ele.find('.num1 .text_mode').attr('val_tag');
		
		ele.find('[name="td_data"]:visible:not(.num1)').each(function(a,b){
			single_json['station'] = station;
			if(station=='5'){
				var times = ele.find('.loop_time .text_mode').html();
				var beginStep = ele.find('.start_process .text_mode').html();
				var endStep = ele.find('.end_process .text_mode').html();

				single_json['times'] = times;
				single_json['beginStep'] = beginStep;
				single_json['endStep'] = endStep;

			}else if(station=='6'){
				end_flag =true ;
			}else{
				var value = $(b).find('.text_mode').html();
	            if(value!==undefined && value!==''){
					var current = $(b).attr('class').split('num')[1];
					var json_name = station_array[current]; 
					single_json[json_name]= value;
	            }
			}
		});

		return {
			'flag':end_flag,
			'json':single_json
		};
	}

	function get_process_array (){
		var process_array = [];
		var mode = '';
		var ele ;
		var in_empty = 0 ; 	//	 首次遍历到空行开启 in_empty=1 , 遍历到非空时检测in_empty值并获取empty_count追加空行json
		var empty_count = 0;
		var empty_json ={ 'station' : 0 };
		var table_ele = par.ele.find('.table_data');

		table_ele.find('tr').each(function(a,b){
			var mode = $(b).attr('mode');
			if(mode != 'none'){
				if(in_empty){
					console.log(empty_count);
					for(let i=0 ; i<empty_count; ++i){
						process_array.push(empty_json);
					}
					in_empty = 0;
					empty_count = 0 ;
				}
				var single_rel = get_single_json ($(b));
				var flag = single_rel['flag'];
				var single_json = single_rel['json'];
				process_array.push(single_json);
				if(flag){
					return false;
				}
				
			}else{
				in_empty =1 ;
				empty_count++;
			}
		});
		if(process_array.length && process_array[process_array.length-1]['station']!='6'){
			process_array.push({ 'station' : '6' });
		}
		return process_array;
	}

	function pack_json (){
		var process_array = get_process_array();
		// console.log(process_array);return;
		if(process_array.length){
			var contact_check = $('.contact_check').val();
			var temp_supp_point = $('.temp_supp_point').val();
			
			process_array[0][station_array[15]] = contact_check;
			process_array[0][station_array[16]] = temp_supp_point;
		}
	

		var json = {
			    "action": "start",
		};
		json['process'] = process_array;
		return json;
	}

	function validate_data (){
		var rel= {} ;
		var table_ele = par.ele.find('.table_data'); 	//	tbody 元素
		
		var error_array = [] ;
		var valid = true; 

		table_ele.find('tr').each(function(a,b){
			var mode = $(b).attr('mode') ; 
			if(mode!=='none'){
				var station = $(b).find('.num1 .text_mode').attr('val_tag') ; 
				var fill_array= fill_td[station];
				for(let i of fill_array){
					var val = $(b).find('.num'+i+' .text_mode').html()+'';
					// console.log(val);
					// 类型校验 true为类型错误
					var type = false ;
					var tmp;
					tmp = val.replace(/(\d+)|(\.)/g,'');
					if(tmp.length>0){
						console.log(val,'类型错误');
						type = true;
					}
					// console.log(val,val==undefined , val=='');
					if(val==undefined || val=='' || type){
						error_array.push($(b).find('.num'+i));
					}
				}
			}
		});

		if(error_array.length){
			valid = false;
		}
		
		if(valid){
			rel['valid'] = valid;
			rel['error_array'] = [];
		}else{
			rel['valid'] = valid;
			rel['error_array'] = error_array;
		}
		return rel;
	}

	// 获取工步 json包
	// 返回值中flag为false表示校验失败
	function get_json (){
		var flag  = true; 
		var json ={};
		switch_css_all();
		var rel = validate_data();
		// console.log(rel);
		if(rel['valid']){
			var json = pack_json();
		}else{
			flag  = false ;
			var  error_array= rel['error_array'];
			blink_element(error_array);
		}
		return {
			'flag' : flag,
 			'json' : json
		};
	}
	
	// 选中元素对应的td 闪烁
	function blink_element(element_array){
		if(!element_array.length){
			return ;
		}
		for(let ele of element_array){
			if(!(ele.is('td'))){
				ele = ele.closest('td');
			}
			ele.addClass('blink');
			setTimeout(function(){
				ele.removeClass('blink');
			},1000);
		}
	}

	function is_end_exist(ele){
		var res = false; 
		ele.prevAll('tr').each(function(a,b){
			if($(b).find('.num1 .text_mode').attr('val_tag')=='6'){
				res = true ;
				return false ;
			}
		});
		console.log(res);
		return res;
	}

	// 添加行 --显示下拉框--点击第一个单元格
	function num1_click(){
		//清除编辑模式 --切换input为text
		par.ele.find('.table_data tbody td.num1').each(function(a,b){
			$(b).click(function(){		//没有数据 , 添加
				var ele = $(b).closest('tr');
				var mode = ele.attr('mode');
				if(mode !== 'edit'){
					switch_css_all();
					if(mode=='none'){
						var b_end = is_end_exist(ele);
						if(b_end){
							return false;
						}
						switch_css(ele,'3',1);
						select_change(ele,3);
						clear_tr_class(ele);
					
						ele.addClass('station3');
						ele.find('.num1 .editor_mode').val('3');
					}else{
						var station = ele.find('.num1 .text_mode').attr('val_tag');
						switch_css(ele,station,1);
					}
				}else{
					ele.siblings('tr[mode="edit"]').each(function(c,d){
						console.log($(b));
						var station = $(b).find('.num1 .text_mode').attr('val_tag');
						switch_css($(b),station,0);
					});
					
				}
			});
		});
	}



	// 编辑行 -- 点击其他单元格
	function other_td_click(){
		par.ele.find('.table_data tbody td[name="td_data"]:not(.num1)').each(function(a,b){
			$(b).click(function(){
				var ele = $(b).closest('tr');
				if(ele.attr('mode')=='none'){
					switch_css_all();
					// c(1);
				}

				if(ele.attr('mode')=='text'){
					var station  = ele.find('.num1 .text_mode').attr('val_tag');
					// c(2);
					switch_css_all();
					switch_css(ele,station,1);
				}
			});
		}); 
	}


	// 事件绑定
	function binding_event(ele){
		if(ele===undefined){
			// 右键响应
			context_click();
			// 双击事件
			num1_click();
			other_td_click();
			keyboard_event();
			num11_change();
			// 改变下拉框
			change_event();
		}else{
			
		}
		
	}

	function keyboard_event(){
		par.ele.find('td').on('keydown',function(e){
			if(e.keyCode==13){
				obj.switch_css_all();
			}
		})
	}

	// 初始化 界面和方法
	function init_table(){
		// 绘制ui
		init_ui();
		// 绑定事件
		binding_event();
	}

	
// ===========================function===========================
	function get_o_html(){
		var o_html='<div class="size table table-bordered text-center table_title " style="margin-bottom: 0px; height:10%;">\
	 			<div class="w h">';
	    for(let i =0;i< obj_ui_conf['col']*1;++i){
	        o_html+='<span data_log="'+i+'" class=" h title_span num'+i+' " >\
	    		<b class="ver_center h">'+obj_ui_conf['title'][i]+'</b>\
	    	</span>';
	    }
	    o_html+='</div></div>';
	    o_html+='<div class="fleft" style="border-bottom: 1px #EBEBEB solid;overflow-y: auto;height:88%">\
		   		<table class="table table_data h table-bordered text-center" style="margin-bottom: 0px;"><tbody ></tbody>\
		    	</table>\
		  	  </div>';
	    return o_html;
	}

	function get_html_body(){
        	var html_body='';
        	var html_body_one_row ='<tr mode="none"><td class="td_id"style="height:'+obj.row_height+'px"></td>';
	        for (let i = 1; i <=obj_ui_conf['col']*1-1; i++) {
	            if(i == 1){
	                html_body_one_row+='<td name="td_data" class="num'+i+'"style="height:'+obj_ui_conf['row_height']+'px;line-height:'+obj_ui_conf['row_height']+'px">'+obj_ui_conf['select_html']+obj_ui_conf['select_html_2']+'</td>';
	            }else if(i==obj_ui_conf['col']-1){
	                html_body_one_row+='<td name="td_data" class="num'+i+'"style="height:'+obj_ui_conf['row_height']+'px;line-height:'+obj_ui_conf['row_height']+'px">'+obj_ui_conf['alert_type']+obj_ui_conf['select_html_2']+'</td>';
	            }else{
	                html_body_one_row+='<td name="td_data" class="num'+i+'"style="height:'+obj_ui_conf['row_height']+'px;line-height:'+obj_ui_conf['row_height']+'px">'+obj_ui_conf['input_html']+obj_ui_conf['select_html_2']+'</td>';
	            }
	        }

	        html_body_one_row+=obj_ui_conf['loop_type'];

	        return html_body_one_row;
    }
   

	function data_obj(station){
		var obj_model={};
		switch(station){
			case 3:
				obj_model={
					"id" : "" ,
		            "contact_check": "",
		            "temp_supp_point": "",
		            "station": "",
		            "time": "",
		            "upperVoltage": "",
		            "lowerVoltage": "",
		            "time_over": ""
		        };break;
			case 2:
				obj_model={
					"id" : "" ,
		            "contact_check": "",
		            "temp_supp_point": "",
		            "station": "",
		            "time": "",
		            "current": "",
		            "upperVoltage": "",
		            "lowerVoltage": "",
		            "stop_DeltaVoltage": "",
		            "stop_Current": "",
		            "stop_Capacity": "",
		            "cct_begin": "",
		            "cct_end": "",
		            "time_over": ""
		        };break;
			case 4:
				obj_model={
					"id" : "" ,
		            "contact_check": "",
		            "temp_supp_point": "",
		            "station": "",
		            "time": "",
		            "current": "",
		            "upperVoltage": "",
		            "lowerVoltage": "",
		            "stop_Capacity": "",
		            "time_over": ""
		        };break;
			case 1:
				obj_model= {
					"id" : "" ,
		            "contact_check": "",
		            "temp_supp_point": "",
		            "station": "",
		            "time": "",
		            "current": "",
		            "upperVoltage": "",
		            "lowerVoltage": "",
		            "stop_DeltaVoltage": "",
		            "stop_Capacity": "",
		            "time_over": ""
		        };break;
			case 5:
				obj_model= {
					"id" : "" ,
		            "station": "",
		            "beginStep": "",
		            "endStep": ""
		        };break;
			default:;
		}
		return obj_model;
	}

	
	// 仅显示num1 -num11 的 text_mode
	function clear_ui(ele){
		clear_tr_class(ele);
		ele.find('[name="td_data"]').show();
		ele.find('[name="td_data"] .text_mode').show();
		ele.find('[name="td_data"] .editor_mode').hide();
		ele.find('.num12').hide();
	}

	// 清除行数据 & val_tag
	function clear_data(ele){

		ele.find('[name="td_data"] .text_mode').html('');
		ele.find('[name="td_data"]:not(.num1) .text_mode').each(function(a,b){
			if($(b).attr('val_tag')!==undefined){
				$(b).attr('val_tag','');
			}
		});

		ele.find('[name="td_data"] .editor_mode').val('');
	}

	// 清洗数据
	function  reprocessing_obj(obj){
		for (var i = 0; i <obj.length; i++) {
			$.each(obj[i],function(j,v){
				if(v==undefined){
					obj[i][j]="";
				}
			});
		}
		return obj;
	}
	// 右键-- 增加 / 删除
	function manipulate_tr (par ,selector){
		var html = get_html_body();
		if(par=='add'){
			selector.after(html);
			var next_tr = selector.next('tr');
			init_tr(next_tr);
			binding_event();

    		update_table_idx();
    		obj_fn.binding_event();
    		obj_ui_conf.len++;
		}else{
			// 删除6 解锁
	        selector.remove(); 
			var ele_tr_count= obj_ui_conf.ele.find('.table_data tbody tr').length;
			// console.log(ele_tr_count,obj_ui_conf.row*1,ele_tr_count<(obj_ui_conf.row*1));
			if(ele_tr_count<(obj_ui_conf.row*1)){
				obj_ui_conf.ele.find('.table_data tbody tr:last').after(html);
				obj_ui_conf.ele.find('.table_data tbody tr:last').find('td').each(function(a,b){
					$(b).find('.text_mode,.editor_mode').hide();
				});
			}
			update_table_idx();
			obj_fn.binding_event();
            obj_ui_conf.len--;
		}
	}

	// 右键响应 debug
	function context_click(){
		var context_menu = [
            {
                icon: 'glyphicon-plus',
                text: '插入工步',
                action: function(e, selector) { 
                	manipulate_tr('add' ,selector);

					
                }
            },{
                icon: 'glyphicon-trash',
                text: '删除工步',
                action: function(e, selector) {
                	manipulate_tr('del' ,selector);
                }
            }
        ];
	    // 阻止预定义右键行为
	    context.init({preventDoubleContext: true});
	    // 绑定事件
	    context.attach(".table_data tbody tr", context_menu);
	}


	// 更新工步号 
	function update_table_idx(){
		par.ele.find('.table_data tbody tr').each(function(a,b){
           $(b).find(".td_id").each(function(c,d){
           		$(d).html(a+1);
           });
        });
	}
	return obj;
}



// 常用工步表
function init_history_table(par){
	var obj= {};
	par.ele= par.ele;
	if(par.ele.length){
		par.ele.html('');
	}else{
		swal('常用工步表指定元素不存在','','error');
		return;
	}
	obj.hei = par.ele.height();
	obj.title = par.title;
	obj.max_tr = par.max_tr;
	obj.per_hei = obj.hei /obj.max_tr + "px";
	// var i="";
	obj.tr_html = '<div class="w t_tr" style="height:' + obj.per_hei + '"><div class="bor pull-left text-center h ver_center id_text" style="width: 25%;"></div><div class="bor pull-left select_text  text-center ver_center  h" style="width: 75%;"></div></div>';

	obj.init=function init(){
		var ohtml="";
		ohtml+=' <div class="table_history_head" style="width: 90%;margin: 2% auto;height: 6%"> <div class="w t_tr" style="height:31.2px"><div class="bor pull-left text-center h ver_center id_text bold" style="width: 25%;">';
		ohtml+=obj.title[0];
		ohtml+='</div> <div class="bor pull-left text-center ver_center h bold" style="width: 75%;">';
		ohtml+=obj.title[1];
		ohtml+='</div> </div> </div>';
		ohtml+='<div class="table_history_data bor" style="height: 92%;overflow-y: auto;width: 90%;margin: 2% auto ;"></div>';
	
		par.ele.html(ohtml);

	    par.ele.find('.table_history_data').append('<div class="w t_tr first_tr sel_show" style="height:' + obj.per_hei + '"><div class="bor pull-left text-center h ver_center id_text" style="width: 25%;">1</div><div class="bor pull-left  text-center ver_center select_text h" style="width: 75%;"></div></div>');
	    for (let i = 2; i <=obj.max_tr; ++i) {
	        par.ele.find('.table_history_data').append('<div class="w t_tr" style="height:' + obj.per_hei + '"><div class="bor pull-left text-center h ver_center id_text" style="width: 25%;">' + i + '</div><div class="bor pull-left select_text  text-center ver_center  h" style="width: 75%;"></div></div>');
	    }
	}
	obj.init();
	
	//val 结果json
	obj.redraw_table = function redraw_table(val) {
		par.ele.find('.table_history_data').find('.t_tr .select_text').each(function(a,b){
			if(val[a]!=undefined){
				$(b).html(val[a][1]);
				$(b).attr('data_id',val[a][0]);
			}else{
				return false;
			}
		});
	}

	// 删除当前行并 补全tr 到obj.row 行
    obj.remove_pad_tr=function remove_pad_tr(selector){
        selector.remove();
		var ele_tr_count= $('.table_history_data').find('.t_tr').length;
		if(ele_tr_count<obj.max_tr){
			par.ele.find('.table_history_data .t_tr:last').after(obj.tr_html);
			obj.update_table_idx();
		}
	}

	// 更新工步号 
	obj.update_table_idx =function update_table_idx(){
		par.ele.find('.table_history_data .t_tr').each(function(a,b){
           $(b).find(".id_text").each(function(c,d){
           		$(d).html(a+1);
           });
        });
	}
    return obj;
}
